package com.stm.bi.core.util;


import com.stm.bi.model.DatatablePage;
import com.stm.bi.model.RowData;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 报表导出工具类
 *
 * @author zyp
 */
public class ReportExporterUtils {

    /**
     * 读取所有数据转成List<Map>
     *
     * @param datatablePage
     * @return
     */
    static public List<Map> getExporterDataList(DatatablePage datatablePage) {
        List<Map> maps = new ArrayList<>();

        if (datatablePage != null && datatablePage.getDatas() != null) {
            List<RowData> dataList = datatablePage.getDatas();
            for (HashMap map : dataList) {
                // 区分是否是树形结构
                if (map instanceof RowData) {
                    RowData rowData = (RowData) map;
                    Map m = new HashMap(rowData);
                    maps.add(m);
                    if (rowData.getChildren() != null && !rowData.getChildren().isEmpty()) {
                        List<RowData> childList = rowData.getChildren();
                        List<Map> treeMaps = getTreeDataMapList(childList);
                        maps.addAll(treeMaps);
                    }
                } else {
                    maps.add(map);
                }
            }
        }
        return maps;
    }

    /**
     * 递归查询所有子节点的数据
     *
     * @param dateList
     * @return
     */
    static public List<Map> getTreeDataMapList(List<RowData> dateList) {
        List<Map> maps = new ArrayList<>();

        for (RowData data : dateList) {
            Map m = new HashMap(data);
            maps.add(m);

            if (data.getChildren() != null && !data.getChildren().isEmpty()) {
                List<Map> childrenMaps = getTreeDataMapList(data.getChildren());
                maps.addAll(childrenMaps);
            }
        }
        return maps;
    }
}
